package com.qulai.amazon_us.service.basic.impl;

import java.util.Date;
import java.util.List;

import com.qulai.amazon_us.domain.amazon_us.BasicProductMap;
import com.qulai.amazon_us.domain.basic.Image;
import com.qulai.amazon_us.domain.basic.Product;
import com.qulai.amazon_us.domain.vo.ImageApprovalVo;
import com.qulai.amazon_us.service.amazon_us.IBasicProductMapService;
import com.qulai.amazon_us.service.basic.IImageService;
import com.qulai.amazon_us.service.basic.IProductService;
import com.qulai.common.utils.DateUtils;
import com.qulai.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qulai.amazon_us.mapper.basic.ImageApprovalMapper;
import com.qulai.amazon_us.domain.basic.ImageApproval;
import com.qulai.amazon_us.service.basic.IImageApprovalService;

/**
 * 图片审核Service业务层处理
 * 
 * @author hunan
 * @date 2025-09-19
 */
@Service
public class ImageApprovalServiceImpl implements IImageApprovalService 
{
    @Autowired
    private ImageApprovalMapper imageApprovalMapper;


    @Autowired
    private IImageService imageService;

    /**
     * 查询图片审核
     * 
     * @param id 图片审核主键
     * @return 图片审核
     */
    @Override
    public ImageApproval selectImageApprovalById(Long id)
    {
        return imageApprovalMapper.selectImageApprovalById(id);
    }

    /**
     * 查询图片审核列表
     * 
     * @param imageApproval 图片审核
     * @return 图片审核
     */
    @Override
    public List<ImageApproval> selectImageApprovalList(ImageApproval imageApproval)
    {
        return imageApprovalMapper.selectImageApprovalList(imageApproval);
    }

    @Override
    public List<ImageApprovalVo> selectImageApprovalVoList(ImageApproval imageApproval)
    {
        List<ImageApprovalVo> list = imageApprovalMapper.selectImageApprovalVoList(imageApproval);
        for (ImageApprovalVo imageApprovalVo : list){
            Image image = imageService.getImageBySku(imageApprovalVo.getSku());
            if (image != null){
                imageApprovalVo.setOriginImage(imageApprovalVo.getOriginImage() == null ? "" : imageApprovalVo.getOriginImage() + "?len=" + countNonEmptyImageUrls(image));
            }
        }
        return list;
    }

    @Override
    public List<ImageApproval> selectImageApprovalListBySku(String sku){
        return imageApprovalMapper.selectImageApprovalListBySku(sku);
    }

    @Override
    public int approval(List<ImageApproval> list) {
        int row = 0;
        for (ImageApproval imageApproval : list){
            imageApproval.setApprovalUserId(SecurityUtils.getUserId());
            imageApproval.setUpdateTime(new Date());
            row += imageApprovalMapper.updateImageApproval(imageApproval);
        }
        return row;
    }

    /**
     * 新增图片审核
     * 
     * @param imageApproval 图片审核
     * @return 结果
     */
    @Override
    public int insertImageApproval(ImageApproval imageApproval)
    {
        imageApproval.setCreateTime(DateUtils.getNowDate());
        return imageApprovalMapper.insertImageApproval(imageApproval);
    }

    /**
     * 修改图片审核
     * 
     * @param imageApproval 图片审核
     * @return 结果
     */
    @Override
    public int updateImageApproval(ImageApproval imageApproval)
    {
        imageApproval.setUpdateTime(DateUtils.getNowDate());
        return imageApprovalMapper.updateImageApproval(imageApproval);
    }

    /**
     * 批量删除图片审核
     * 
     * @param ids 需要删除的图片审核主键
     * @return 结果
     */
    @Override
    public int deleteImageApprovalByIds(Long[] ids)
    {
        return imageApprovalMapper.deleteImageApprovalByIds(ids);
    }

    /**
     * 删除图片审核信息
     * 
     * @param id 图片审核主键
     * @return 结果
     */
    @Override
    public int deleteImageApprovalById(Long id)
    {
        return imageApprovalMapper.deleteImageApprovalById(id);
    }

    private int countNonEmptyImageUrls(Image tmp) {
        int count = 0;
        if (!tmp.getMainImage().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl1().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl2().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl3().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl4().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl5().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl6().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl7().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl8().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl9().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl10().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl11().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl12().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl13().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl14().trim().isEmpty()) count++;
        if (!tmp.getOtherImageUrl15().trim().isEmpty()) count++;

        return count;
    }
}
